package com.lzxuni.modules.dataAcquisition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.enterprise.entity.EnterpriseInfo;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import com.lzxuni.modules.productdisplay.entity.ProductDisplay;
import com.lzxuni.modules.productdisplay.service.ProductDisplayService;
import com.lzxuni.modules.system.entity.BaseArea;
import com.lzxuni.modules.system.service.BaseAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.lzxuni.modules.productdisplay.entity.ProductDisplay;

import javax.sound.sampled.Line;
import java.util.List;

@RestController
@RequestMapping("/Traceability")
public class TraceabilityController {
    @Autowired
    private BaseAreaService areaService;
    @Autowired
    private BaseAreaService baseAreaService;
    @Autowired
    private ProductDisplayService productService;
    @Autowired
    private EnterpriseInfoService enterpriseInfoService;


    @RequestMapping(value = "/suYuan/{code}/{id}/{type}",method = RequestMethod.GET)
    public ModelAndView Index(@PathVariable("code") String code,@PathVariable("id") String id, @PathVariable("type") String type) {
        ModelAndView mv = new ModelAndView("/product/suYuan1");
        if(StringUtils.isEmpty(id) || id.equals("0")){
            ProductDisplay product = productService.getOne(new QueryWrapper<ProductDisplay>().eq("traceability_code", code));
            id = product == null ? "0" : product.getYwId();
        }
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping(value = "/product/{code}/{id}/{type}")
    public ModelAndView commodity(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/product");
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping(value = "/qiYe/{code}/{id}/{type}")
    public ModelAndView qiYe(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/qiYe");

        ProductDisplay Info = productService.getOne(new QueryWrapper<ProductDisplay>().eq("YW_ID", id));

        if (Info != null) {
            String cid = Info.getAffiliatedEnterprises();
            EnterpriseInfo info = enterpriseInfoService.getOne(new QueryWrapper<EnterpriseInfo>().eq("YW_ID", cid));

            if (info.getProvinceId() != null && info.getProvinceId().length() != 0) {
                System.out.println("省：" + baseAreaService.getById(info.getProvinceId()).getAreaName());
                mv.addObject("provinceId", baseAreaService.getById(info.getProvinceId()).getAreaName());
            }
            if (info.getCityId() != null && info.getCityId().length() != 0) {
                System.out.println("市：" + baseAreaService.getById(info.getCityId()).getAreaName());
                mv.addObject("cityId", baseAreaService.getById(info.getCityId()).getAreaName());
            }
            if (info.getCountyId() != null && info.getCountyId().length() != 0) {
                System.out.println("区：" + baseAreaService.getById(info.getCountyId()).getAreaName());
                mv.addObject("countyId", baseAreaService.getById(info.getCountyId()).getAreaName());
            }
            if (info.getTownId() != null && info.getTownId().length() != 0) {
                System.out.println("镇：" + baseAreaService.getById(info.getTownId()).getAreaName());
                mv.addObject("townId", baseAreaService.getById(info.getTownId()).getAreaName());
            }
            if (info.getVillageId() != null && info.getVillageId().length() != 0) {
                System.out.println("村：" + baseAreaService.getById(info.getVillageId()).getAreaName());
                mv.addObject("villageId", baseAreaService.getById(info.getVillageId()).getAreaName());
            }
            mv.addObject("info", info);
            mv.addObject("id", id);
            mv.addObject("code", code);
            mv.addObject("type", type);
        }
        return mv;
    }

    @RequestMapping(value = "/jianCe/{code}/{id}/{type}")
    public ModelAndView jianCe(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/jianCe");
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping(value = "/fangWei/{code}/{id}/{type}")
    public ModelAndView fangWei(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/fangWei");
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping(value = "/pingJia/{code}/{id}/{type}")
    public ModelAndView pingJia(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/pingJia");
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping(value = "/yaoGan/{code}/{id}/{type}")
    public ModelAndView yaoGan(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/yaoGan");
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping(value = "/zLTS/{code}/{id}/{type}")
    public ModelAndView zLTS(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/zhiLiangTouSu");
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping(value = "/fWJZ/{code}/{id}/{type}")
    public ModelAndView fWJZ(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/fangWeiJuZheng");
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping(value = "/cPPJ/{code}/{id}/{type}")
    public ModelAndView cPPJ(@PathVariable("code") String code, @PathVariable String id, @PathVariable String type) {
        ModelAndView mv = new ModelAndView("/product/chanPinPingJia");
        mv.addObject("id", id);
        mv.addObject("code", code);
        mv.addObject("type", type);
        return mv;
    }
}
